package com.kaibes.module.message;

import java.math.BigInteger;
import java.util.List;

import org.springframework.stereotype.Service;

import com.kaibes.orm.core.core.Page;
import com.kaibes.orm.curd.QueryBuilder;
import com.kaibes.orm.curd.UpdateBuilder;
import com.kaibes.web.api.ResponseServiceImpl;

@Service
public class MessageService extends ResponseServiceImpl<Message> {

    public Page<Message> pageByTitleLikeOrContentLike(int pageIndex, int pageSize, String search) {
        return super.page(pageIndex, pageSize, QueryBuilder.newBuilder().like("title", search).build(), 
                QueryBuilder.newBuilder().like("content", search).build());
    }
    
    public Page<Message> pageByUserIdAndRead(int pageIndex, int pageSize, Integer userId, boolean isReaded) {
        return super.pageDesc(pageIndex, pageSize, "id", QueryBuilder.newBuilder().eq("to_id", userId).eq("read", isReaded).build());
    }
    
    public long countByUserIdAndRead(Integer userId, boolean isReaded) {
        return super.count(QueryBuilder.newBuilder().eq("to_id", userId).eq("read", isReaded).build());
    }
    
    public int updateReadByIds(Integer userId, List<BigInteger> ids) {
        return getMapper().update(UpdateBuilder.newBuilder().setValue("read", true).addQuery(QueryBuilder.newBuilder().eq("to_id", userId).inList("id", ids).build()).build());
    }
    
    public int removeByIdAndToId(Object id, Integer toId) {
        return remove(QueryBuilder.newBuilder().eq("id", id).eq("to_id", toId).build());
    }
    
    public int removeByToId(Integer toId) {
        return remove(QueryBuilder.newBuilder().eq("to_id", toId).build());
    }
    
    public int removeByToIdAndRead(Integer toId, boolean read) {
        return remove(QueryBuilder.newBuilder().eq("to_id", toId).eq("read", read).build());
    }
    
}
